Practical-07
Mobile Application
Practical List
Create an android program that will have a UI where the main screen has a list of cars. On selecting any car, the next screen will display the number of cars in the gallery with car information.
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add a
ListViewelement to the layout with the following attributes:android:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"
- Create a new XML file called
car_item.xmlin theres/layoutdirectory. - Open the
car_item.xmlfile and add the following code to define the layout for each car item in the list:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/carImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/car_icon" />
<TextView
android:id="@+id/carNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginStart="16dp" />
</LinearLayout> - Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
setupCarListto populate the list view with car items:private void setupCarList() {
ListView listView = findViewById(R.id.listView);
List<Car> carList = getCarList();
CarListAdapter adapter = new CarListAdapter(this, carList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Car car = (Car) parent.getItemAtPosition(position);
showCarDetails(car);
}
});
} - Create a new class called
Carto represent a car object with the following attributes:String nameint imageResIdint galleryCountString information
- Create a new class called
CarListAdapterthat extendsArrayAdapter<Car>and implements the necessary methods to display the car items in the list view using thecar_item.xmllayout. - Implement the
getCarListmethod to return a list of car objects with dummy data. - Create a new method called
showCarDetailsto display the car details in the next screen:private void showCarDetails(Car car) {
Intent intent = new Intent(this, CarDetailsActivity.class);
intent.putExtra("car", car);
startActivity(intent);
} - Create a new activity called
CarDetailsActivitywith a layout fileactivity_car_details.xml. - Open the
CarDetailsActivity.javafile. - Inside the
onCreatemethod, add the following code to get the car object from the intent and display the car details:Car car = getIntent().getParcelableExtra("car");
TextView carNameTextView = findViewById(R.id.carNameTextView);
TextView galleryCountTextView = findViewById(R.id.galleryCountTextView);
TextView informationTextView = findViewById(R.id.informationTextView);
carNameTextView.setText(car.getName());
galleryCountTextView.setText(String.valueOf(car.getGalleryCount()));
informationTextView.setText(car.getInformation()); - Run the application on an Android device or emulator.
- The main screen will display a list of cars.
- Select any car from the list to navigate to the next screen and display the car details.
Congratulations! You have successfully created an Android program that has a UI where the main screen has a list of cars. On selecting any car, the next screen displays the number of cars in the gallery with car information.